package com.lsy.android.pingpang;

import com.lsy.android.pingpang.view.SettingView;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.NotificationType;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengUpdateAgent;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);	
		setContentView(R.layout.main_activity);
		//自动更新
		UmengUpdateAgent.update(this);
		//错误报告
		MobclickAgent.onError(this);
		//反馈信息给用户
		UMFeedbackService.enableNewReplyNotification(this, NotificationType.NotificationBar);

		Button localButton1 = (Button) findViewById(R.id.button1);
		Button localButton2 = (Button) findViewById(R.id.button2);
		Button localButton3 = (Button) findViewById(R.id.button3);
		Button localButton4 = (Button) findViewById(R.id.button4);
		Button localButton5 = (Button) findViewById(R.id.button5);
		Button localButton6 = (Button) findViewById(R.id.button6);
		localButton1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				Intent localIntent = new Intent(MainActivity.this,
						GameActivity.class);
				localIntent.putExtra("gameStyle", 0);
				MobclickAgent.onEvent(MainActivity.this, "人机对战");
				startActivity(localIntent);
			}
		});
		localButton2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				Intent localIntent = new Intent(MainActivity.this,
						GameActivity.class);
				localIntent.putExtra("gameStyle", 1);
				MobclickAgent.onEvent(MainActivity.this, "双人对战");
				startActivity(localIntent);
			}
		});
		localButton3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				Intent intent = new Intent(MainActivity.this,
						BluetoothActivity.class);
				MobclickAgent.onEvent(MainActivity.this, "蓝牙对战");
				startActivity(intent);
			}
		});
		localButton4.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				showSettingDialog();
			}
		});
		localButton5.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				showExitDialog();
			}
		});
		localButton6.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				UMFeedbackService.openUmengFeedbackSDK(MainActivity.this);
			}
		});
	}

	private void showExitDialog() {
		final Dialog dialog = new Dialog(this, R.style.exit_dialog);
		View view = LayoutInflater.from(this).inflate(R.layout.exit_dialog,
				null);
		dialog.setContentView(view);
		Button ok = (Button) view.findViewById(R.id.btn_ok);
		ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				MainActivity.this.finish();
			}
		});
		Button cancel = (Button) view.findViewById(R.id.btn_cancel);
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	private void showSettingDialog() {
		Dialog dialog = new Dialog(this, R.style.setting_dialog);
		dialog.setContentView(new SettingView(this, dialog));
		dialog.show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitDialog();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

}
